Використовуючи запропоновані дані, створіть складне представлення даних з наступними елементами
import altair as alt
import pandas as pd
import geopandas as gpd
df = pd.read_csv('population_trends.csv')
df = df[df.region != 'Ukraine']
df.head()
| region | year | rate | |
|---|---|---|---|
| 31 | Crimea | 1989 | 3.9 |
| 32 | Crimea | 1990 | 2.5 |
| 33 | Crimea | 1991 | 0.9 |
| 34 | Crimea | 1992 | -0.7 |
| 35 | Crimea | 1993 | -2.7 |
df_2019 = df[df.year == 2019]
df_2019.head()
| region | year | rate | |
|---|---|---|---|
| 61 | Crimea | 2019 | NaN |
| 92 | Vinnytsya | 2019 | -7.9 |
| 123 | Volyn | 2019 | -2.8 |
| 154 | Dnipropetrovs'k | 2019 | -8.9 |
| 185 | Donets'k | 2019 | NaN |
ukraine = gpd.read_file('ukraine.json')
ukraine.drop(['GID_0', 'NAME_0', 'GID_1', 'VARNAME_1', 'NL_NAME_1',
'TYPE_1', 'ENGTYPE_1', 'CC_1', 'HASC_1'], axis = 1, inplace = True)
ukraine.columns = ['region', 'geometry']
ukraine.head() #26
| region | geometry | |
|---|---|---|
| 0 | Cherkasy | MULTIPOLYGON (((31.32614 48.74507, 31.31716 48... |
| 1 | Chernihiv | MULTIPOLYGON (((33.09283 50.50966, 33.09261 50... |
| 2 | Chernivtsi | MULTIPOLYGON (((24.93280 47.72794, 24.93301 47... |
| 3 | Crimea | MULTIPOLYGON (((33.79291 44.39153, 33.79465 44... |
| 4 | Dnipropetrovs'k | MULTIPOLYGON (((33.93176 47.48407, 33.92332 47... |
merged = ukraine.merge(df_2019, on = 'region')
merged['rate'] = merged['rate'].fillna('NaN')
merged.head()
| region | geometry | year | rate | |
|---|---|---|---|---|
| 0 | Cherkasy | MULTIPOLYGON (((31.32614 48.74507, 31.31716 48... | 2019 | -10.0 |
| 1 | Chernihiv | MULTIPOLYGON (((33.09283 50.50966, 33.09261 50... | 2019 | -12.5 |
| 2 | Chernivtsi | MULTIPOLYGON (((24.93280 47.72794, 24.93301 47... | 2019 | -3.1 |
| 3 | Crimea | MULTIPOLYGON (((33.79291 44.39153, 33.79465 44... | 2019 | NaN |
| 4 | Dnipropetrovs'k | MULTIPOLYGON (((33.93176 47.48407, 33.92332 47... | 2019 | -8.9 |
base = alt.Chart(merged).mark_geoshape(
fill = 'lightgray', stroke = 'black', strokeWidth = 0.4)
color = alt.Chart(merged).mark_geoshape(
stroke = 'black', strokeWidth = 0.4,
).encode(
color=alt.condition(
alt.datum['rate'] == 'NaN' ,
alt.value('lightgrey'),
alt.Color('rate:Q', bin = alt.Bin(step = 2),
legend = alt.Legend(title = "Rate", direction = 'horizontal', orient= "bottom-left"),
scale = alt.Scale(scheme = 'lightmulti', reverse=True))),
tooltip = [alt.Tooltip('region:N'), alt.Tooltip('rate:Q')]
).properties(width = 700, height = 450, title = 'Population growth/reduction rate for 2019')
chart_1 = alt.layer(base, color).encode()
chart_1
chart_2 = alt.Chart(df).mark_line(
).encode(
x = alt.X('year:Q', title ='Year',
scale = alt.Scale(zero = False),
axis = alt.Axis(format = 'k', labelFontSize=12)),
y = alt.Y('rate:Q', title = 'Rate',
axis = alt.Axis(labelFontSize=12)),
detail = alt.Detail('region:N'),
tooltip = [alt.Tooltip('region:N'), alt.Tooltip('rate:Q'), alt.Tooltip('year:Q')],
color = alt.value("#ff9933"),
size = alt.value(2)
).properties(width = 700, height = 450, title = 'Population growth/reduction by regions')
chart_2
alt.hconcat(chart_1,
chart_2,
resolve = alt.Resolve(scale=alt.LegendResolveMap(color=alt.ResolveMode('independent')))
).configure_legend(titleFontSize=15, labelFontSize=14
).configure_axis(titleFontSize=15
).configure_title(fontSize=20)